package com.lovedice;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
import com.lovedice.database.DBAdapter;
import com.lovedice.type.Action;

public class DiceBonus extends DiceListConfiguration {

	private static final int DICE_FORM_ADD = 0;
	private static final int DICE_FORM_EDIT_OR_DELETE = 1;
	private Resources resources;
	private DBAdapter dbAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		resources = getResources();
		dbAdapter = new DBAdapter(getApplicationContext());
		dbAdapter.open();
        carregarLista(dbAdapter.retrieveAllBonus());
        dbAdapter.close();
	}

	@Override
	protected void adicionar() {
		Intent intent = new Intent(this, DiceForm.class);
		intent.putExtra("id", 0);
		intent.putExtra("nome", "");
		intent.putExtra("action", Action.CREATE.getAction());
    	startActivityForResult(intent, DICE_FORM_ADD);
	}

	@Override
	protected void acoesEditarOuExcluir(String nome) {
		dbAdapter.open();
		Cursor bonusByName = dbAdapter.getBonusByName(nome);
		dbAdapter.close();

		Intent intent = new Intent(this, DiceForm.class);
		intent.putExtra("id", bonusByName.getInt(0));
		intent.putExtra("nome", nome);
		intent.putExtra("action", Action.UPDATE);
		startActivityForResult(intent, DICE_FORM_EDIT_OR_DELETE);
	}
	
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	if(resultCode==RESULT_OK && requestCode==DICE_FORM_ADD){
    		String nome = data.getStringExtra("nome");
    		dbAdapter.open();
    		if (!nome.equals("")) {
    			dbAdapter.insertBonus(nome);
    			Toast.makeText(this, resources.getString(R.string.dices_set_added), Toast.LENGTH_SHORT).show();
    		} else {
    			Toast.makeText(this, resources.getString(R.string.dices_set_empty_name), Toast.LENGTH_SHORT).show();
    		}
    		carregarLista(dbAdapter.retrieveAllBonus());
    		dbAdapter.close();
    	}
    	else if(resultCode==RESULT_OK && requestCode==DICE_FORM_EDIT_OR_DELETE){
    		int rowId = data.getIntExtra("id", 0);
    		String nome = data.getStringExtra("nome");
    		dbAdapter.open();
    		if (!nome.equals("") && rowId > 0) {
    			if(dbAdapter.updateBonus(rowId, nome))
    				Toast.makeText(this, resources.getString(R.string.dices_set_updated), Toast.LENGTH_SHORT).show();
    		} else {
    			Toast.makeText(this, resources.getString(R.string.dices_set_empty_name), Toast.LENGTH_SHORT).show();
    		}
    		carregarLista(dbAdapter.retrieveAllBonus());
    		dbAdapter.close();
    	}
    	else if(resultCode==RESULT_FIRST_USER && requestCode==DICE_FORM_EDIT_OR_DELETE){
    		int rowId = data.getIntExtra("id", 0);
    		String nome = data.getStringExtra("nome");
    		excluir(rowId, nome);
    	}
    	else  if(resultCode==RESULT_CANCELED){
    		dbAdapter.open();
    		carregarLista(dbAdapter.retrieveAllBonus());
    		dbAdapter.close();
    	}
    }

	private void excluir(final int rowId, String nome) {
		new AlertDialog.Builder(this)
		.setTitle(R.string.dices_deleted_title)
		.setMessage(resources.getString(R.string.dices_deleted_message) + "\n\""+nome+"\"" )
		.setPositiveButton(R.string.dices_deleted_button_confirm, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dbAdapter.open();
				dbAdapter.deleteBonus(rowId);
				carregarLista(dbAdapter.retrieveAllBonus());
				dbAdapter.close();
				Toast.makeText(getApplicationContext(), resources.getString(R.string.dices_set_deleted), Toast.LENGTH_SHORT).show();
				dialog.dismiss();
			}
		})
		.setNegativeButton(R.string.dices_deleted_button_cancel, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dbAdapter.open();
				carregarLista(dbAdapter.retrieveAllBonus());
				dbAdapter.close();
				dialog.dismiss();
			}
		})
		.show();
	}
}
